using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using ConfigWS = Dimok.Config.imaging.WS;
using ConfigDB = Dimok.Config.db;
using Dimok.Data.Database;
//using Dimok.Documents;

////////////////////////////////////////////////////////////////////////////////////////////////////
// namespace: Dimok.Images
//
// summary:	.
////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Dimok.Images{

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Works with pages in image folders in database (t_PageTypeID) </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class Pages{

		#region ImageFolder ===================

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderID">		Identifier for the folder. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID){
			return ImageFolder(connection,applID,folderID,PageTypes.DEFAULT);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">		The connection. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="contentType">	Type of the content. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID,string contentType){
			return ImageFolder(connection,applID,folderID,Enums.GetPageType(contentType));
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderID">		Identifier for the folder. </param>
		/// <param name="pageType">		Type of the page. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID,PageTypes pageType){
			return ImageFolder(connection,applID,folderID,pageType,"");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">		The connection. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="contentType">	Type of the content. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID,string contentType,object[] pageNumbers){
			return ImageFolder(connection,applID,folderID,Enums.GetPageType(contentType),pageNumbers);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">		The connection. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID,object[] pageNumbers){
			return ImageFolder(connection,applID,folderID,PageTypes.DEFAULT,pageNumbers);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">		The connection. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageType">			Type of the page. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID,PageTypes pageType,object[] pageNumbers){
			return ImageFolder(connection,applID,folderID,pageType,new StringList(pageNumbers).Join(","));
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="Exception">	Thrown when exception. </exception>
		///
		/// <param name="connection">		The connection. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageType">			Type of the page. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int folderID,PageTypes pageType,string pageNumbers){
			if( pageType == PageTypes.DEFAULT )pageType = Enums.GetPageType(ConfigWS.DefaultContentType);
			DataRowCollection drcPages = SQLHelperEx.ExecuteDataset(connection,"s_Pages_Get_Ex",applID,folderID,pageType,pageNumbers).Tables[0].Rows;
			if( drcPages.Count == 0 )return null;
			switch( pageType ){
				case PageTypes.TIFF:
					//try{
						throw new Exception("TIFF type is not implemented.");
						//return Tiff.JoinPages(CheckTIFF(drcPages));
					//}catch( NotTiffException ){ 
					//	return ImageFolder(connection,applID,folderID,PageTypes.PDF,pageNumbers);
					//}
				case PageTypes.PDF:
						try {
							return Dimok.ABCPdf.CombinePDF(CheckPDF(connection, drcPages));
						} catch (OverflowException) {
							return Tiffer.Tiff.JoinPages(drcPages);
						}
					//return AcrobatClient.CombinePDF(CheckPDF(connection,drcPages));
				default:
					throw new Exception("MSWord is not supported type for images.");
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Return joint pages of specified type (TIFF - default) </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="Exception">	Thrown when exception. </exception>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderIDs">	The folder i ds. </param>
		/// <param name="pageType">		Type of the page. </param>
		///
		/// <returns>	File in array. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] ImageFolder(string connection,int applID,int[] folderIDs,PageTypes pageType){
			if( pageType == PageTypes.DEFAULT )pageType = Enums.GetPageType(ConfigWS.DefaultContentType);
			switch( pageType ){
				case PageTypes.TIFF:
					throw new Exception("TIFF type is not implemented.");
					/*
					ArrayList alImages = new ArrayList();
					foreach( int folderID in folderIDs ){
						if( pageType == PageTypes.DEFAULT )pageType = Enums.GetPageType(ConfigWS.DefaultContentType);
						ImageFolderConvert(applID,folderID);
						DataRowCollection drcPages = SQLHelperEx.ExecuteDataset(connection,"s_Pages_Get_Ex",applID,folderID,pageType).Tables[0].Rows;
						foreach( DataRow row in drcPages )
							alImages.Add((byte[])row["Data"]);
					}
					return Tiff.JoinPages((byte[][])alImages.ToArray(typeof(byte[])));
					*/
				case PageTypes.PDF:
					ArrayList alPDF = new ArrayList();
					for( int i = 0; i < folderIDs.Length; i++ ){
						byte[] imagePDF = ImageFolder(connection,applID,folderIDs[i],pageType,"");
						if( imagePDF != null )alPDF.Add(imagePDF);
					}
					return Dimok.ABCPdf.CombinePDF((byte[][])alPDF.ToArray(typeof(byte[])));
				default:
					throw new Exception("MSWord is not supported type for images.");
			}
		}
		// private static DataRowCollection CheckTIFF(DataRowCollection rows){
		// 	byte[][] pages = new byte[rows.Count][];
		// 	for( int i = 0; i < rows.Count; i++ ){
		// 		PageTypes dbPageType = Convert.IsDBNull(rows[i]["Data"]) ? PageTypes.UNKNOWN :
		// 		Enums.GetPageType((byte[])rows[i]["Data"]);
		// 		if( dbPageType != PageTypes.TIFF )
		// 			throw new NotTiffException();
		// 			//throw new Exception("Requested image format (TIFF) does not exist in Database.");
		// 			//rows[i]["Data"] = FileManager.GetBytesFromFile(HttpContext.Current.Server.MapPath(@"\
		// 			images\NoImage.tif"));
		// 	}
		// 	return rows;
		// } 
		private static byte[][] CheckPDF(string connection,DataRowCollection rows){
			byte[][] pages = new byte[rows.Count][];
			for( int i = 0; i < rows.Count; i++ )
				pages[i] = (int)rows[i]["PageTypeID"] == (int)PageTypes.TIFF ? UpdatePDFPage(connection,rows[i]) : (byte[])rows[i]["Data"];
			return pages;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the pdf page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="row">				The row. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private static byte[] UpdatePDFPage(string connection,DataRow row){
			//throw new Exception("UpdatePDFPage is not implemented.");
			byte[] pdfPage = ABCPdf.TiffToPDF((byte[])row["Data"]);
			//byte[] pdfPage = PDF.TiffToPDF((byte[])row["Data"]);
			ImagePageUpdate(connection,(int)row[ConfigDB.ApplID],(int)row["ImageTypeID"],PageTypes.PDF,(int)row["PageNumber"],pdfPage);
			return pdfPage;
		}
		#endregion ImageFolder ===================

		#region ImagePageUpdate ===================

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image page update. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="pageType">		Type of the page. </param>
		/// <param name="pageNumber">	The page number. </param>
		/// <param name="page">				The page. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePageUpdate(string connection,int applID,int folderID,PageTypes pageType,int pageNumber,byte[] page){
			SQLHelperEx.ExecuteNonQuery(connection,"s_Page_Update",applID,folderID,pageType,pageNumber,page);
		}
		#endregion ImagePageUpdate ===================

		#region ImagePagesInsert ======================

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Insert tiff file to Image Folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="fileName">		Filename of the file. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePagesInsert(string connection,int applID,int folderID,string fileName,int pageAfter){
			ImagePagesInsert(connection,applID,folderID,FileManager.GetBytesFromFile(fileName),pageAfter);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Insert tiff file to Image Folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="file">				The file. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePagesInsert(string connection,int applID,int folderID,byte[] file,int pageAfter){
			ImagePagesInsert(connection,applID,folderID,pageAfter,Dimok.ABCPdf.Slice(file));
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Insert tiff file to Image Folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="pages">			pages. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		///
		/// ### <param name="pageType">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePagesInsert(string connection,int applID,int folderID,byte[][] pages,int pageAfter){
			ImagePagesInsert(connection,applID,folderID,pageAfter,pages);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Insert tiff file to Image Folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		/// <param name="files">			A variable-length parameters list containing files. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[MethodImplAttribute(MethodImplOptions.Synchronized)]
		public static void ImagePagesInsert(string connection,int applID,int folderID,int pageAfter,params byte[][][] files){
			using(SqlConnection c = new SqlConnection(connection)){
				c.Open();
				SqlTransaction tran = c.BeginTransaction(IsolationLevel.ReadCommitted);
				ImagePagesInsert(tran,applID,folderID,pageAfter,files);
				tran.Commit();
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Insert tiff file to Image Folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="tran">				The tran. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		/// <param name="files">			A variable-length parameters list containing files. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePagesInsert(SqlTransaction tran,int applID,int folderID,int pageAfter,params byte[][][] files) {
			for( int i = 0; i < files[0].Length; i++ ){
				ArrayListEx alPages = new ArrayListEx();
				foreach( byte[][] pages in files )
					if( pages.Length > i && pages[i] != null && pages[i].Length > 0 )
						alPages.Add(pages[i]);
				pageAfter = ImagePageInsert(tran,applID,folderID,pageAfter,alPages);
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image page insert. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="tr">					The tr. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderID">		Identifier for the folder. </param>
		/// <param name="page">				The page. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ImagePageInsert(SqlTransaction tr,int applID,int folderID,byte[] page,int pageAfter){
			return ImagePageInsert(tr,applID,folderID,pageAfter,page);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image page insert. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="tr">					The tr. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderID">		Identifier for the folder. </param>
		/// <param name="pageAfter">	Position in Image Folder. </param>
		/// <param name="pages">			pages. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ImagePageInsert(SqlTransaction tr,int applID,int folderID,int pageAfter,params byte[][] pages){
			ArrayListEx alParams = new ArrayListEx(new object[]{applID,folderID,pageAfter,null});
			foreach( byte[] page in pages )
				alParams.AddRange(new object[]{Enums.GetPageType(page),page});
			return (int)SQLHelperEx.ExecSP(tr,"s_Page_Insert_Ex",alParams.ToArray());
			//return (int)SQLHelperEx.ExecSP(tr,"s_Page_Insert_Ex",applID,folderID,pageAfter,null,pageType,page);
		}
		#endregion ImagePagesInsert ======================

		#region Others =====================

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static DataSet ImageList(string connection){return ImageList(connection,0);}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static DataSet ImageList(string connection,int applID){
			return SQLHelperEx.ExecuteDatasetEx(connection,"sp__img_IE_FoldersList",applID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image page count. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderID">		Identifier for the folder. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ImagePageCount(string connection,int applID,int folderID){
			return (int)SQLHelperEx.ExecuteScalarEx(connection,"SELECT dbo.PageCount(@ApplID,@FolderID)",applID,folderID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Delete page range from DB. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		/// <param name="folderID">		Identifier for the folder. </param>
		/// <param name="pageNumber">	The page number. </param>
		///
		/// <returns>	Number of pages after deletion. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ImagePagesDelete(string connection,int applID,int folderID,int pageNumber) {
			return ImagePagesDelete(connection,applID,folderID,new object[]{pageNumber});
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Delete page range from DB. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">		The connection. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	Number of pages after deletion. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ImagePagesDelete(string connection, int applID, int folderID, params object[] pageNumbers) {
			using(SqlConnection c = new SqlConnection(connection)){
				c.Open();
				SqlTransaction tran = c.BeginTransaction(IsolationLevel.ReadCommitted);
				int pageCount = ImagePagesDelete(tran,applID, folderID, pageNumbers);
				tran.Commit();
				return pageCount;
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Delete page range from DB. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="tran">					The tran. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	Number of pages after deletion. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ImagePagesDelete(SqlTransaction tran,int applID,int folderID,params object[] pageNumbers) {
			return (int)SQLHelperEx.ExecSP(tran,"s_DeletePages_Ex",applID,folderID,new StringList(pageNumbers).Join(","));
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image pages move. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">				The connection. </param>
		/// <param name="applID">						. </param>
		/// <param name="folderID_From">		The folder identifier from. </param>
		/// <param name="pageNumbers">			The page numbers. </param>
		/// <param name="folderID_To">			The folder identifier to. </param>
		/// <param name="pageNumber_After">	The page number after. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePagesMove(string connection, int applID, int folderID_From, object[] pageNumbers, int folderID_To, int pageNumber_After) {
			SQLHelperEx.ExecuteNonQuery(connection,"s_MovePages_Ex",applID,folderID_From,new StringList(pageNumbers).Join(","),folderID_To,pageNumber_After);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Image pages replace. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="connection">	The connection. </param>
		/// <param name="applID">			. </param>
		/// <param name="folderID">		. </param>
		/// <param name="files">			A variable-length parameters list containing files. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ImagePagesReplace(string connection, int applID, int folderID, params byte[][][] files) {
			using (SqlConnection c = new SqlConnection(connection)) {
				c.Open();
				SqlTransaction tran = c.BeginTransaction(IsolationLevel.ReadCommitted);
				ImagePagesDelete(tran, applID, folderID);
				ImagePagesInsert(tran, applID, folderID, 0, files);
				tran.Commit();
			}
		}
		#endregion Others =====================

		#region Helpers ======================
		#endregion

		#region Privates ====================
		/*
		private static byte[][] Slice(byte[] pages){return Slice(pages,PageTypes.UNKNOWN);}
		private static byte[][] Slice(byte[] pages,PageTypes pageType){
			if( pageType == PageTypes.UNKNOWN )pageType = Enums.GetPageType(pages);
			switch( pageType ){
				case PageTypes.TIFF: return Tiff.SliceTiff(pages);
				case PageTypes.PDF: return PDF.Slice(pages);
				default:
					throw new Exception("Unsupported PageType:"+pageType+Environment.NewLine+Encoding.ASCII.GetString(pages,0,128));
			}
		}
		*/
		#endregion
	}
}
